package cn.fl.seccar.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.seccar.service.IAucSplUsrService;
import cn.fl.seccar.transform.AucSplUsrVOTransform;
import cn.fl.seccar.vo.AucSplUsrVO;
import fl.seccar.facade.auccar.api.IAucCarAddApiService;
import fl.seccar.facade.auccar.api.IAucCarModifyApiService;
import fl.seccar.facade.auccar.api.IAucCarQueryApiService;
import fl.seccar.facade.auccar.condition.AucSplUsrQC;
import fl.seccar.facade.auccar.dto.AucSplUsrDTO;

/**
 * @Description: 商家用户表 客户端服务层接口实现  
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class AucSplUsrServiceImpl implements IAucSplUsrService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IAucSplUsrService.class);
	
	@Autowired
	private IAucCarQueryApiService aucCarQueryApiService;
	@Autowired
	private IAucCarAddApiService aucCarAddApiService;
	@Autowired
	private IAucCarModifyApiService aucCarModifyApiService;
	
	@Override
	public AucSplUsrVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		AucSplUsrDTO dto = aucCarQueryApiService.getAucSplUsrDTO(id);
		return AucSplUsrVOTransform.toVO(dto);
	}

	@Override
	public Long add(AucSplUsrVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		this.addCheckInfo(entity);
		return this.aucCarAddApiService.addAucSplUsr(AucSplUsrVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.aucSplUsrApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(AucSplUsrVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		this.modifyCheckInfo(entity);
		return this.aucCarModifyApiService.modifyAucSplUsr(AucSplUsrVOTransform.toDTO(entity));
	}
	
	/**
	 * 修改校验
	 */
	private void addCheckInfo(AucSplUsrVO entity) throws BaseException{
		StringBuffer tipInfo = new StringBuffer();
		//验证姓名是否有同名字的
		AucSplUsrQC qc = new AucSplUsrQC();
		qc.setSplNm(entity.getSplNm());
		qc.setIsDel(BaseConstant.IS_YESNO_NO);
		List<AucSplUsrDTO> list = this.aucCarQueryApiService.searchAucSplUsrList(qc);
		boolean pass = true;//是否可以操作
		if(CollectionUtils.isNotEmpty(list)){
			pass = false;
			tipInfo.append(" 商户名称已存在,不能操作!");
		}
		//验证手机号是否有相同的
		AucSplUsrQC cpqc = new AucSplUsrQC();
		cpqc.setUsrNm(entity.getUsrNm());
		cpqc.setIsDel(BaseConstant.IS_YESNO_NO);
		List<AucSplUsrDTO> cplist = this.aucCarQueryApiService.searchAucSplUsrList(cpqc);
		if(CollectionUtils.isNotEmpty(cplist)){
			pass = false;
			tipInfo.append(" 登录帐号已存在,不能操作!");
		}
		
		if(!pass){
			BaseLogUtils.info(logger, "addCheckInfo", tipInfo.toString(), CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, tipInfo.toString());
		}
	}

	/**
	 * 修改校验
	 */
	private void modifyCheckInfo(AucSplUsrVO entity) throws BaseException{
		StringBuffer tipInfo = new StringBuffer();
		
		//验证姓名是否有同名字的
		AucSplUsrQC qc = new AucSplUsrQC();
		qc.setSplNm(entity.getSplNm());
		qc.setIsDel(BaseConstant.IS_YESNO_NO);
		List<AucSplUsrDTO> list = this.aucCarQueryApiService.searchAucSplUsrList(qc);
		boolean pass = true;//是否可以操作
		if(CollectionUtils.isNotEmpty(list)){
			for (AucSplUsrDTO aucSplUsrDTO : list) {
				Long tmpId = aucSplUsrDTO.getId();
				if(!tmpId.equals(entity.getId())){
					pass = false;
					tipInfo.append(" 商户名称已存在,不能操作!");
				}
			}
		}
		//验证手机号是否有相同的
		AucSplUsrQC cpqc = new AucSplUsrQC();
		cpqc.setUsrNm(entity.getUsrNm());
		cpqc.setIsDel(BaseConstant.IS_YESNO_NO);
		List<AucSplUsrDTO> cplist = this.aucCarQueryApiService.searchAucSplUsrList(cpqc);
		if(CollectionUtils.isNotEmpty(cplist)){
			for (AucSplUsrDTO aucSplUsrDTO : cplist) {
				Long tmpId = aucSplUsrDTO.getId();
				if(!tmpId.equals(entity.getId())){
					pass = false;
					tipInfo.append(" 登录帐号已存在,不能操作!");
				}
			}
		}
		
		if(!pass){
			BaseLogUtils.info(logger, "modifyCheckInfo", tipInfo.toString(), CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, tipInfo.toString());
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		page = this.aucCarQueryApiService.searchAucSplUsrPage(page);
		List<AucSplUsrDTO> list = (List<AucSplUsrDTO>)page.getResultList();
		page.setResultList(AucSplUsrVOTransform.toVOList(list));
		return page;
	}

	@Override
	public boolean modifyIsVliad(AucSplUsrVO entity) throws BaseException {
		return this.aucCarModifyApiService.modifyAucSplUsr(AucSplUsrVOTransform.toDTO(entity));
	}
}
